<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <ul>
      <li>a</li>
      <li>b</li>
      <li>c</li>
      <li>d</li>
      <li>e</li>
      <p>666</p>
    </ul>
  </head>
  <body>
    <button>点击添加</button>
    <script>
      // 普通方法
      //   var lis = document.querySelectorAll("li");
      //   lis.forEach(function (li) {
      //     li.onclick = function () {
      //       console.log("点击了");
      //     };
      //   });
      // 事件委托
      var ulEle = document.querySelector("ul");
      ulEle.onclick = function (e) {
        var e = e || window.e;
        var target = e.target || e.srcElement;
        // console.log(target);
        // console.log(target.nodeName);
        if (target.nodeName == "LI") {
          target.style.color = "red";
        }
      };
      var buttonEle = document.querySelector("button");
      buttonEle.onclick = function () {
        var lis = document.createElement("li");
        lis.innerHTML = "添加的li";
        ulEle.appendChild(lis);
      };
    </script>
  </body>
</html>
